package com.huatai.bimonitor.rabbitmq;

import com.alibaba.nacos.api.utils.NetUtils;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: ganghua
 * @description: 配置发布订阅
 * @author: songxiude
 * @create: 2024-09-05 10:29
 **/
@Configuration
public class FanountConfig {

	@Value("${server.port}")
	private String port;

	//交换机声明
	@Bean
	public FanoutExchange fanoutExchange() {
		return new FanoutExchange("fanoutExchange");
	}

	/**
	 * 实例存活通知
	 * @return
	 */
	@Bean
	public FanoutExchange serverExchange() {
		return new FanoutExchange("serverExchange");
	}

	//声明队列1
	@Bean
	public Queue fanountQeueueMqtt() {
		String ipport = NetUtils.localIP().replace(".", "_") +"_"+port;
		return new Queue("fanountQeueueMqtt_" + ipport);
	}

	/**
	 * 实例存活通知
	 * @return
	 */
	@Bean
	public Queue serverQeueueMqtt() {
		String ipport = NetUtils.localIP().replace(".", "_") +"_"+port;
		return new Queue("serverQeueueMqtt_" + ipport);
	}


	@Bean
	public Binding bingQueue1ToExchange() {
		return BindingBuilder.bind(fanountQeueueMqtt()).to(fanoutExchange());
	}

	/**
	 * 交换机绑定
	 * @return
	 */
	@Bean
	public Binding bingQueue2ToExchange() {
		return BindingBuilder.bind(serverQeueueMqtt()).to(serverExchange());
	}
}
